package top.itaso.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import top.itaso.app.net.ApiSystem4BodyInterceptor;
import top.itaso.lib.alog.ALog;
import top.itaso.lib.alog.ALogLevel;
import top.itaso.lib.net.NetInit;

/**
 * @Description:
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Gitee https://gitee.com/itsdf07
 * @Date 2021/11/30
 */
public class MyApplication extends Application {
    private static final String TAG = "MyApplication";
    private static Context mContext;

    private Application.ActivityLifecycleCallbacks mActivityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
            Log.v(TAG, "onActivityCreated: >>>>>>activity=" + activity + ",savedInstanceState=" + savedInstanceState);
        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {
            Log.v(TAG, "onActivityStarted: >>>>>>activity=" + activity);
        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {
            Log.v(TAG, "onActivityResumed: >>>>>>activity=" + activity);
        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {
            Log.v(TAG, "onActivityPaused: >>>>>>activity=" + activity);
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {
            Log.v(TAG, "onActivityStopped: >>>>>>activity=" + activity);
        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
            Log.v(TAG, "onActivitySaveInstanceState: >>>>>>activity=" + activity + ",outState=" + outState);
        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {
            Log.v(TAG, "onActivityDestroyed: >>>>>>activity=" + activity);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks);
        ALog.init()
                .setLogLevel(ALogLevel.FULL)
                .setShowThreadInfo(true)
                .setMethodCount(2)
                .writeLog(this)
                .writeCrash(this)
        ;
        NetInit.init(this)
                .withApiHost("http://api.itaso.top/")
                .withInterceptors(
                        new ApiSystem4BodyInterceptor())
                .showLog(true)
//                .withInterceptorParamsSet(new IInterceptorParamsSet() {
//                    @Override
//                    public HashMap<String, String> headerParams() {
//                        HashMap<String, String> headers = new HashMap<>();
//                        headers.put("pkgName", mContext.getPackageName());
//                        headers.put("clientOs", "1");
//                        headers.put("channel", "00001");
//                        headers.put("version", AppUtils.getAppVersionName(mContext, null));
//                        return headers;
//                    }
//
//                    @Override
//                    public HashMap<String, String> bodyParams() {
//                        HashMap<String, String> bodys = new HashMap<>();
//                        bodys.put("pkgName", mContext.getPackageName());
//                        bodys.put("clientOs", "1");
//                        bodys.put("channel", "00001");
//                        bodys.put("version", AppUtils.getAppVersionName(mContext, null));
//                        return bodys;
//                    }
//                })
                .configure();
    }


    @Override
    public void onTerminate() {
        // 程序终止的时候执行
        Log.e(TAG, "onTerminate: >>>>>>");
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        // 低内存的时候执行
        Log.e(TAG, "onLowMemory: >>>>>>");
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        // 程序在内存清理的时候执行
        Log.e(TAG, "onTrimMemory: >>>>>>level=" + level);
        super.onTrimMemory(level);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d(TAG, "onConfigurationChanged: >>>>>>newConfig=" + newConfig.toString());
    }

    public static Context getContext() {
        return mContext;
    }
}
